using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class admin_PersonManagement : admin_BaseUserControl
{
    Person person = new Person();
    PersonDAO daoPerson = new PersonDAO();
    public string DOB
    {
        get { return person.DateOfBirth.ToShortDateString(); }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        ActiveAccording = "person";
        try
        {
            int mode = int.Parse(Request["mode"]);
            Util util = new Util();
            cmbCountry.DataSource = util.GetCountryList();
            cmbCountry.DataBind();
            if (mode == 1)
            {
                //mode: edit
                lnkAdd.Visible = false;
                lnkUpdate.Visible = true;
                lnkDelete.Visible = true;
                int id = int.Parse(Request["id"]);
                person = daoPerson.GetPerson(id);
                LoadData();
            }
            else
            {
                //mode: add new
                txtFirstName.Text = "";
                txtLastName.Text = "";
                txtEmail.Text = "";
                txtPhone.Text = "";
                txtNote.Text = "";
                txtState.Text = "";
                cmbCountry.SelectedIndex = 0;
                txtCity.Text = "";
                txtAddress.Text = "";
                
                lnkAdd.Visible = true;
                lnkUpdate.Visible = false;
                lnkDelete.Visible = false;
            }
        }
        catch (Exception ex)
        {
            Response.Redirect("Default.aspx?ctrl=personel");
        }
    }
    /// <summary>
    /// Method to load data into form
    /// </summary>
    public void LoadData()
    {        
        txtFirstName.Text = person.FirstName;
        txtLastName.Text = person.LastName;
        txtAddress.Text = person.Address;
        txtCity.Text = person.City;
        txtState.Text = person.State;
        txtEmail.Text = person.Email;        
        txtPhone.Text = person.Phone;
        txtNote.Text = person.Note;
        if (person.Gender) radMale.Checked = true;
        else radFemale.Checked = true;
        //set country
        
        int index = 0;
        person.Country = person.Country.ToLower();

        foreach (ListItem item in cmbCountry.Items)
        {
            if (item.Text.ToLower() == person.Country)
            {
                cmbCountry.SelectedIndex = index;
                break;
            }
            index++;
        }
    }
    /// <summary>
    /// Method for adding new person information
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void lnkAdd_Click(object sender, EventArgs e)
    {
        Person ps= new Person();
        PersonDAO daoPerson= new PersonDAO();
        ps.FirstName = txtFirstName.Text;
        ps.LastName = txtLastName.Text;
        ps.Address = txtAddress.Text;     
        ps.DateOfBirth = DateTime.Parse(Request["txtDateOfBirth"]);
        ps.Email = txtEmail.Text;
        ps.City = txtCity.Text;
        ps.Country = cmbCountry.SelectedItem.Text;
        ps.Phone = txtPhone.Text;        
        ps.City = txtCity.Text;
        ps.State = txtState.Text;        
        ps.ModifiedDate = DateTime.Today;
        ps.ModifiedBy = 1;
        ps.Gender = radMale.Checked;
        if (daoPerson.Insert(ps) > 0)
        {
           ShowMessage( "Successfull. New person info has been added.");
           
        }
        else ShowMessage("Error has occured. Action is failed. Please try again");
       
    }
    /// <summary>
    /// Update current personal information
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void lnkUpdate_Click(object sender, EventArgs e)
    {
        person.FirstName = txtFirstName.Text;
        person.LastName = txtLastName.Text;
        person.DateOfBirth = DateTime.Parse(Request["txtDateOfBirth"]);
        if (radMale.Checked) person.Gender = true;
        else person.Gender = false;
        person.Address = txtAddress.Text;
        person.Country = cmbCountry.SelectedItem.Text;
        person.State = txtState.Text;
        person.City = txtCity.Text;
        person.Email = txtEmail.Text;
        person.Phone = txtPhone.Text;
        person.Note = txtNote.Text;
        person.ModifiedBy = 1;
        person.ModifiedDate = DateTime.Today;
        if (daoPerson.Update(person) > 0)
        {
            ShowMessage("Personal information is updated successfully");
        }
        else ShowMessage("Error has occured when trying to update personal information");
    }
    /// <summary>
    /// Delete current personal information
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void lnkDelete_Click(object sender, EventArgs e)
    {
        if (daoPerson.Delete(person.Id) > 0)
        {
            ShowMessage("Specified personal information is deleted successfully");
            ClearFields();
        }
        else ShowMessage("Error has occured when trying to delete specified personal information");
    }
    /// <summary>
    /// Clear text fields value
    /// </summary>
    public void ClearFields()
    {
        txtFirstName.Text = "";
        txtLastName.Text = "";
        txtEmail.Text = "";
        txtAddress.Text = "";       
        txtState.Text = "";
        cmbCountry.SelectedIndex = 0;
        txtCity.Text = "";
        txtNote.Text = "";
    }
}
